﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CopyIt
{
    public partial class DeviceForm : Form
    {
        List<string> ListDrives = new List<string>();
        List<PairsParameters> Params;
        string[] Drives = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N", "O",
                          "P", "Q","R","S","T","U","V","W","X","Y","Z"};

        public DeviceForm(ref List<PairsParameters> Params)
        {
            this.Params = Params;
            InitializeComponent();
            CreateDrivesList();
            ChangeLetter();
        }

        public void ChangeLetter()
        {
            foreach (PairsParameters pairs in Params)
            {
                if (!Directory.Exists(pairs.DestFolder))
                    SearchNewDestination(pairs);
            }
            if (lsvPairs.Items.Count > 0)
            {
                lsvPairs.Columns[0].Width = -1;
                lsvPairs.Columns[1].Width = -1;
            }
            else
            {
                MessageBox.Show("Non ci sono abbinamenti mancanti", "Controllo Abbinamenti",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.DialogResult = DialogResult.Cancel;
            }
        }

        private void SearchNewDestination(PairsParameters pairs)
        {
            string destFolder = Path.GetPathRoot(pairs.DestFolder);
            foreach (string drives in ListDrives)
            {
                string newFolder = string.Concat(drives, pairs.DestFolder.Substring(destFolder.Length));
                if (Directory.Exists(newFolder) && newFolder != pairs.SourceFolder  )
                {
                    AddNewPairs(pairs, newFolder, true);
                    return;
                }
            }
            AddNewPairs(pairs, "Non trovato", false);
        }

        private void AddNewPairs(PairsParameters pairs, string newfolder, bool check)
        {
            ListViewItem lvI = new ListViewItem(pairs.DestFolder);
            ListViewItem.ListViewSubItem suI = lvI.SubItems.Add(newfolder);
            lvI.Checked = check;
            lsvPairs.Items.Add(lvI);
            lvI.Tag = check ? pairs.Guid : "";
            if (!check)
                suI.ForeColor = Color.Red;

        }
        private void CreateDrivesList()
        {
            foreach (DriveInfo dInfo in DriveInfo.GetDrives())
            {
                if (dInfo.IsReady && dInfo.DriveType != DriveType.CDRom)
                    ListDrives.Add(dInfo.Name);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            foreach (PairsParameters pair in Params)
            {
                foreach (ListViewItem lvi in lsvPairs.Items)
                {
                    if (lvi.Checked && lvi.Tag.Equals(pair.Guid))
                    {
                        pair.DestFolder = lvi.SubItems[1].Text;
                        break;
                    }
                }
            }
            this.Close();
        }
    }
}
